package com.zastoupil.pgc.blueprint_pattern.xml.compile;

import java.util.Queue;

/**
 * The AbstractCompiler, and each of its extending classes, is responsible for
 * compiling and evaluating expressions in the scripting language used as a part
 * of the blueprint design pattern.
 * 
 * @author Peter Zastoupil
 * 
 */
public abstract class AbstractCompiler {

	private Queue<AbstractExpression> expressionQueue;

	/**
	 * @return the expression Queue
	 */
	public Queue<AbstractExpression> getExpressionQueue() {
		return expressionQueue;
	}

	/**
	 * @param expressionQueue
	 *            the expression Queue to set
	 */
	public void setExpressionQueue(Queue<AbstractExpression> expressionQueue) {
		this.expressionQueue = expressionQueue;
	}

	/**
	 * Parses a given input string into an AbstractExpression object and returns
	 * the object. If <code>addToQueue</code> is <code>true</code>, the parsed
	 * AbstractExpression will be added to the internal expression queue of the
	 * AbstractCompiler
	 * 
	 * @param input
	 *            the string to be parsed into an AbstractExpression object
	 * @param addToQueue
	 * @return the AbstractExpression object parsed from the given input string,
	 *         or <code>null</code> if the string is empty
	 */
	public AbstractExpression parse(String input, boolean addToQueue) {
		AbstractExpression parsedExpression = this.parse(input);
		if (addToQueue) {
			this.getExpressionQueue().add(parsedExpression);
		}
		return parsedExpression;
	}

	/**
	 * Parses a given input string into an AbstractExpression object and returns
	 * the object
	 * 
	 * @param input
	 *            the string to be parsed into an AbstractExpression object
	 * @return the AbstractExpression object parsed from the given input string,
	 *         or <code>null</code> if the string is empty
	 * @throws IllegalArgumentException
	 *             if the given input string is not empty but cannot be parsed
	 */
	public abstract AbstractExpression parse(String input)
			throws IllegalArgumentException;

}
